<template>
  <el-row type="flex" class="row-bg" justify="center">
    <el-col :xl="6" :lg="7">
      <h2>欢迎访问后台管理系统</h2>
      <el-image :src="require('@/assets/03.jpg')"></el-image>
      <p>6666666666</p>
      <p>9999999999</p>
    </el-col>
    <el-col :span="1">
      <el-divider direction="vertical"></el-divider>
    </el-col >
    <el-col :xl="6" :lg="7">
      <el-form :model="loginForm" :rules="rules" ref="loginForm" label-width="100px" class="demo-loginForm">
        <el-form-item label="用户名" prop="username" style="width: 380px">
          <el-input v-model="loginForm.username"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password" style="width: 380px">
          <el-input v-model="loginForm.password"></el-input>
        </el-form-item>
        <el-form-item label="验证码" prop="code" style="width: 380px">
          <el-input v-model="loginForm.code" style="width: 150px; float: left"></el-input>
          <el-image :src="captchaImg" class="captchaImg" @click="getCaptcha"></el-image>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="submitForm('loginForm')">登录</el-button>
          <el-button @click="resetForm('loginForm')">重置</el-button>
        </el-form-item>
      </el-form>
    </el-col>
  </el-row>
</template>

<script>
import qs from 'qs'
export default {
  name: "Login",
  data() {
    return {
      //表单数据
      loginForm: {
        username: '',
        password: '',
        code: '',
        token: ''
      },
      //校验规则
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 3, message: '长度至少为3个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 3, message: '长度至少为3个字符', trigger: 'blur' }
        ],
        code: [
          { required: true, message: '请输入验证码', trigger: 'blur' },
          { min: 4, max:4, message: '验证码长度为4个字符', trigger: 'blur' }
        ]
      },
      //验证码图片(base64编码)
      captchaImg: ""
    }
  },
  methods: {
    //提交表单
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.$axios.post('/login?' + qs.stringify(this.loginForm))
              .then(res => {
                console.log('/login')
                console.log(res)
                const jwt = res.headers['authorization']
                console.log("jwt:" + jwt)
                //调用vuex中的方法，保存后端返回的jwt数据并存到vuex和localStorage中
                this.$store.commit('SET_TOKEN', jwt)
                this.$router.push('/main')
              })
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    //重置表单
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    // 获取验证码
    getCaptcha() {
      this.$axios.get('/captcha')
          .then(res => {
                console.log('/captcha')
                console.log(res)
                this.loginForm.token = res.data.data.token
                this.captchaImg = res.data.data.base64Img
              }
          )
    }
  },
  created() {
    this.getCaptcha()
  }
}
</script>

<style scoped>
.el-row {
  background-color: #fafafa;
  height: 100%;
  display: flex;
  align-items: center;
  text-align: center;
  justify-content: center;
}

.el-divider {
  height: 200px;
}

.captchaImg {
  float: left;
  margin-left: 8px;
  border-radius: 4px;
}
</style>
